package com.nf.demo.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author lwj
 * @date 2025-02-25
 * @vesion 1.0.0
 */
public class DruidUtils {
    /**
     * 数据源对象
     */
    private static DataSource dataSource;
    /**
     * 驱动信息
     */
    private static String driver;
    /**
     * 连接地址信息
     */
    private static String url;
    /**
     * 连接的用户名
     */
    private static String username;
    /**
     * 连接的密码
     */
    private static String password;

    /**
     * 获取数据源对象
     */
    public static DataSource getDataSource(){
        if(dataSource == null){
            // 开启连接
            initDataSource();
        }
        return dataSource;
    }

    /**
     * 初始化数据源
     */
    private static void initDataSource(){
        // 读取配置文件对象
        Properties properties = new Properties();
        try{
            // 加载配置文件
            properties.load(DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            // 读取配置文件信息
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch(Exception e){
            System.out.println("初始化数据源失败！");
            throw new RuntimeException(e);
        }
    }

    /**
     * 从连接池获取连接对象
     * @return 连接对象
     */
    public static Connection getConnection(){
        try{
            return getDataSource().getConnection();
        } catch (SQLException e) {
            throw new RuntimeException("开启数据库发送异常！",e);
        }
    }

    /**
     * 关闭连接，回收连接池
     * @param connection 连接对象
     */
    public static void closeConnection(Connection connection){
        try{
            if(connection != null){
                connection.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException("关闭数据库发送异常！",e);
        }
    }

    public static void main(String[] args) {
        Connection connection = DruidUtils.getConnection();
        System.out.println(connection);
        Connection connection1 = DruidUtils.getConnection();
        System.out.println(connection1);
        Connection connection2 = DruidUtils.getConnection();
        System.out.println(connection2);
        Connection connection3 = DruidUtils.getConnection();
        System.out.println(connection3);
        Connection connection4 = DruidUtils.getConnection();
        System.out.println(connection4);
        Connection connection5 = DruidUtils.getConnection();
        System.out.println(connection5);
        Connection connection6 = DruidUtils.getConnection();
        System.out.println(connection6);
        Connection connection7 = DruidUtils.getConnection();
        System.out.println(connection7);
        Connection connection8 = DruidUtils.getConnection();
        System.out.println(connection8);
        Connection connection9 = DruidUtils.getConnection();
        System.out.println(connection9);
        // Connection connection10 = DruidUtils.getConnection();
        // System.out.println(connection10);
        Connection connection11 = DruidUtils.getConnection();
        System.out.println(connection11);
    }
}
